package com.practice.book.service;

import com.practice.book.constant.Constants;
import com.practice.book.mapper.UserMapper;
import com.practice.book.model.UserInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-01-20
 * Time: 15:52
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public Boolean checkUserNameAndPassword(String userName, String password, HttpSession session) {
        //1.校验参数格式
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        //2.从数据库校验账号和密码是否存在
        UserInfo userInfo = userMapper.selcetUser(userName);
        if(userInfo == null) {
            return false;
        }
        if(userInfo.getUserName().equals(userName) && userInfo.getPassword().equals(password)) {
            //Session中的内容，取决于需要什么...
            session.setAttribute(Constants.SESSION_USER_KEY,userName);
            return true;
        }
        return false;
    }
}
